<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./vue.js"></script>
  <script src="./vue-router.js"></script>
  <style>
    .router-link-active{
      background:red;
    }
  </style>
</head>
<body>
  <div id="app">
    <router-link to="/detail/1">
      我是新闻1  
    </router-link>
    <router-link to="/detail/2">
      我是新闻2 
    </router-link>
    <router-link to="/detail/3">
      我是新闻2 
    </router-link>

    <!-- 出口 -->
    <router-view></router-view>
  </div>
  <script>
    // /home home组件  /news显示news组件
    
    let News = {
      template:`
        <div>
          <h1>
            新闻页
          </h1>  
        </div>
      `
    }
    let NewsDetail = {
      template:`
        <div>
          <h1>
            我是新闻详情  
          </h1>  
        </div>
      `,
      mounted(){
        console.log(this.$route.params.id)
      },
      watch:{
        "$route"(to,from){
          console.log(to);
          console.log(from)
        }
      }
    }
    const routes = [
    {
        path:"/",
        redirect:"/news"
      },
     {
        path:"/news",
        component:News
      },{
        path:"/detail/:id",
        component:NewsDetail
      }
    ];
    // 创建一个路由对象
    const router = new VueRouter({
      routes //定义路由规则
    });
    // 挂载 路由
    let vm = new Vue({
      el:"#app",
      router
    })
    /*
    hash路由
      根据 地址 的hash值得变化 来 切换不同组件
      window.addEvenetListener("hashchange",()=>{
        // 监听地址栏  hash值变化
      })
    */ 
  </script>
</body>
</html>